package com.hzw.controller;

import com.hzw.bean.Article;
import com.hzw.bean.RotationChart;
import com.hzw.dto.ApiResult;
import com.hzw.dto.ArticleResponse;
import com.hzw.service.AdminService;
import com.hzw.service.ArticleService;
import com.hzw.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.hzw.api.Api.CLIENT_ERROR;
import static com.hzw.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/4/5.
 */

@Controller
@CrossOrigin
@RequestMapping("/admin")
public class AdminCtroller {
    @Resource
    private AdminService adminService;
    @Resource
    private ArticleService articleService;

    /**
     * 增加轮播图
     * @param rotationChart
     * @return
     */
    @RequestMapping(value = "/addRotationChart",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addRotationChart(@RequestBody RotationChart rotationChart){
        ApiResult result = new ApiResult();
        try{
            adminService.addRotationChart(rotationChart);
        }catch (Exception e){
            return result.error(SERVER_ERROR,e.getMessage());
        }
        return result;
    }

    /**
     * 删除轮播图
     * @param rotationChart
     * @return
     */
    @RequestMapping(value = "/deleteRotationChartById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteRotationChartById(@RequestBody RotationChart rotationChart){
        ApiResult result = new ApiResult();
        String id = rotationChart.getId();
        if(ObjectUtils.isNullOrEmpty(id)){
            return result.error(CLIENT_ERROR,"id不能为空！");
        }
        try{
            int count = adminService.deleteRotationChartById(id);
            if(count == 1){
                result.setMsg("删除轮播图成功！");
            }else {
                result.setMsg("删除失败：未找到此id对应的轮播图！");
            }
        }catch (Exception e){
            return result.error(SERVER_ERROR,e.getMessage());
        }
        return result;
    }

    /**
     * 修改轮播图
     * @param rotationChart
     * @return
     */
    @RequestMapping(value = "/updateRotationChart",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateRotationChart(@RequestBody RotationChart rotationChart){
        ApiResult result = new ApiResult();
        try{
            adminService.updateRotationChart(rotationChart);
        }catch (Exception e){
            return result.error(SERVER_ERROR,e.getMessage());
        }

        return result;
    }

    /**
     * 获取三张轮播图
     * @return
     */
    @RequestMapping(value = "/selectAllRotationChart",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectAllRotationChart(){
        ApiResult result = new ApiResult();
        try {
            List<RotationChart> rotationChartList = adminService.selectAllRotationChart();
            result.setData(rotationChartList);
        }catch (Exception e){
            return result.error(SERVER_ERROR,e.getMessage());
        }
        return result;
    }

    /**
     * 获取举报数达到30的博客列表
     * @return
     */

    @RequestMapping(value = "/selectChargeFull",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectChargeFull(){
        ApiResult result = new ApiResult();
        try{
            List<ArticleResponse> articleResponseList = articleService.selectChargeFull();
            result.setData(articleResponseList);
        }catch (Exception e){
            return result.error(SERVER_ERROR,e.getMessage());
        }
        return result;
    }

    /**
     * 删除举报数大于等于30的博文
     * @param article
     * @return
     */
    @RequestMapping(value = "/deleteChargeFull",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteChargeFull(@RequestBody Article article){
        ApiResult result = new ApiResult();
        try{
            int count = articleService.deleteChargeFull(article);
            if(count == 1){
                result.setMsg("删除成功！");
            }
        }catch (Exception e){
            return result.error(SERVER_ERROR,e.getMessage());
        }
        return result;
    }




    /**
     *后台推荐列表：根据条件获取文章
     * @return
     */
    @RequestMapping(value = "/selectRecommendArticleByCondition",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectRecommendArticleByCondition(@RequestBody Article article){
        ApiResult result = new ApiResult();
        try{
            List<ArticleResponse> articleResponseList = articleService.selectRecommendArticleByCondition(article);
            result.setData(articleResponseList);
        }catch (Exception e){
            return result.error(SERVER_ERROR,e.getMessage());
        }
        return result;
    }
}
